package hn.cch.dynamic;

import hn.cch.constant.JdbcConstant;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Order(1)
@Aspect
@Component
public class DynamicAspect {

    // @Before("execution(* hn.cch.template.*.*(..))")
    // public void beforeMethod() throws Throwable {
    //     DynamicDataBase.setDataSourceKey(JdbcConstant.MASTER);
    // }

    @Around("@annotation(dynamicAnnotation)")
    public Object beforeAnnotation(ProceedingJoinPoint proceedingJoinPoint, DynamicAnnotation dynamicAnnotation) throws Throwable {
        DynamicDataBase.setDataSourceKey(dynamicAnnotation.name());
        Object object = proceedingJoinPoint.proceed();
        DynamicDataBase.setDataSourceKey(JdbcConstant.MASTER);
        return object;
    }

    // @AfterReturning("execution(* hn.cch.template.*.*(..))")
    // public void afterReturning() throws Throwable {
    //     DynamicDataBase.removeDataSourceKey();
    // }
}
